package com.ym.controller.patient.askDoctor;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.Locker;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.redis.RedisKeyEnum;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.param.CommentRecordParam;
import com.ym.medical.service.IPatientSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dai
 * @create 2019-05-06 13:01
 **/
@Api(tags = "患者端-首页-患者其他功能")
@RestController
@RequestMapping(value = "/patient/ask/patient", headers = Constants.Api.VERSION)
public class PatientController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(PatientController.class);

	@Autowired
	private IPatientSV iPatientSV;

	@Log
	@ApiOperation("扫一扫加关注医生")
	@PostMapping(value = "/attention/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse addAttention(@ApiParam(name = "doctorId", value = "关注的医生id", required = true) @RequestParam long doctorId) {
		Long patientId = ShiroUtil.getLoginId();
		Long aLong = iPatientSV.addAttention(patientId, doctorId);
		return successData(aLong);
	}

	@Log
	@ApiOperation("取消关注")
	@PostMapping(value = "/attention/cancel", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ResponseBody
	public APIResponse cancelAttention(@ApiParam(name = "id", value = "通讯录主键", required = true) @RequestParam long id) {
		int rows = iPatientSV.cancelAttention(id);
		if (rows != 1) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");

	}

	@Log
	@ApiOperation("评价医生保存")
	@Locker(paramExp = "0#orderId?insertEvalDoctor", key = RedisKeyEnum.FREQUENT_OPERATION_KEY, limitTime = 1000, noGetMsg = "操作太频繁，请稍后再试！")
	@PostMapping(value = "/comment/add", consumes = MediaType.APPLICATION_JSON_VALUE)
	public APIResponse insertEvalDoctor(@RequestBody CommentRecordParam commentRecordModel) {
		boolean flag = iPatientSV.insertEvalDoctor(commentRecordModel);
		if (flag) {
			return successMsg("评价医生保存成功！");
		}
		return failMsg("评价医生保存失败!");
	}
}
